package com.school.information.utils;

import com.school.information.core.IResultStatus;
import com.school.information.enums.result.ResultStatusEnum;
import lombok.Data;

import java.io.Serializable;

@SuppressWarnings({"unchecked", "rawtypes"})
@Data
public class ResultUtil<T> implements Serializable {

    public static final ResultUtil SUCCESS_NO_DATA = new ResultUtil(ResultStatusEnum.SUCCESS, null);
    public static final ResultUtil ERROR_ARG = new ResultUtil(ResultStatusEnum.ERROR_ARG, null);
    public static final ResultUtil ERROR_SERVICE = new ResultUtil(ResultStatusEnum.ERROR_SERVICE, null);
    public static final ResultUtil VALIDATE_ERROR = new ResultUtil(ResultStatusEnum.VALIDATE_ERROR, null);

    private Integer code;
    private String msg;
    private T data;

    public ResultUtil() {

    }

    public ResultUtil(IResultStatus resultStatusVO, T data) {
        this.code = resultStatusVO.getCode();
        this.msg = resultStatusVO.getMsg();
        this.data = data;
    }

    public static <T> ResultUtil<T> success(T data) {
        return new ResultUtil<>(ResultStatusEnum.SUCCESS, data);
    }

    public static <T> ResultUtil<T> error(IResultStatus resultStatusVo) {
        return new ResultUtil<>(resultStatusVo, null);
    }
}